package com.zeng.sim.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.zeng.sim.controller.domain.SysMenu;
import com.zeng.sim.controller.domain.response.Result;
import com.zeng.sim.controller.domain.response.ResultTable;
import com.zeng.sim.controller.domain.response.ResultTree;
import com.zeng.sim.controller.vo.SysMenuVo;
import com.zeng.sim.mapper.model.SysMenuEntity;
import com.zeng.sim.service.SysMenuService;
import com.zeng.sim.util.SimConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zengwanlin
 * @since 2023-12-20
 */
@Controller
@RequestMapping("/sys/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping("/list")
    @ResponseBody
    public List<SysMenuVo> list() {
        List<SysMenuEntity> menuEntityList = sysMenuService.list();
        List<SysMenuVo> list = new ArrayList<>(menuEntityList.size());
        for (SysMenuEntity menuEntity : menuEntityList) {
            SysMenuVo menuVo = new SysMenuVo();
            menuVo.setId(menuEntity.getResId());
            menuVo.setParentId(menuEntity.getParentId());
            menuVo.setTitle(menuEntity.getResName());
            menuVo.setType(menuEntity.getResType());
            menuVo.setOpenType(menuEntity.getOpenType());
            menuVo.setIcon(menuEntity.getIcon());
            menuVo.setHref(menuEntity.getResUrl());
            list.add(menuVo);
        }
        return sysMenuService.toUserMenu(list, "0");
    }

    @GetMapping("/main")
    public String main() {
        return "system/menu/main";
    }

    @GetMapping("/add")
    public String add() {
        return "system/menu/add";
    }

    @GetMapping("/edit")
    public String edit(Model model, String resId) {
        model.addAttribute("sysMenu", sysMenuService.getById(resId));
        return "system/menu/edit";
    }

    @DeleteMapping("/remove/{resId}")
    @ResponseBody
    public Result remove(@PathVariable String resId) {
        return Result.decide(sysMenuService.remove(Collections.singletonList(resId)));
    }

    @DeleteMapping("/batchRemove/{resIds}")
    @ResponseBody
    public Result batchRemove(@PathVariable String resIds) {
        return Result.decide(sysMenuService.remove(Arrays.asList(resIds.split(","))));
    }

    @PutMapping("/valid/{resId}")
    @ResponseBody
    public Result valid(@PathVariable String resId) {
        return Result.decide(sysMenuService.valid(resId));
    }

    @PutMapping("/invalid/{resId}")
    @ResponseBody
    public Result invalid(@PathVariable String resId) {
        return Result.decide(sysMenuService.invalid(resId));
    }

    @GetMapping("/data")
    @ResponseBody
    public ResultTable data() {
        List<SysMenuEntity> menuEntityList = sysMenuService.list();
        List<SysMenu> list = new ArrayList<>(menuEntityList.size());
        for (SysMenuEntity menuEntity : menuEntityList) {
            SysMenu sysMenu = new SysMenu();
            sysMenu.setResId(menuEntity.getResId());
            sysMenu.setResName(menuEntity.getResName());
            sysMenu.setResType(menuEntity.getResType());
            sysMenu.setResCode(menuEntity.getResCode());
            sysMenu.setResUrl(menuEntity.getResUrl());
            sysMenu.setOpenType(menuEntity.getOpenType());
            sysMenu.setParentId(menuEntity.getParentId());
            sysMenu.setIcon(menuEntity.getIcon());
            sysMenu.setSort(menuEntity.getSort());
            sysMenu.setValidFlag(menuEntity.getValidFlag());
            sysMenu.setCreateBy(menuEntity.getCreateBy());
            sysMenu.setUpdateBy(menuEntity.getUpdateBy());
            sysMenu.setRemark(menuEntity.getRemark());
            list.add(sysMenu);
        }
        return ResultTable.dataTable(list);
    }

    @GetMapping("/selectParent")
    @ResponseBody
    public ResultTree selectParent(SysMenu sysMenu) {
        List<SysMenu> list = sysMenuService.list(sysMenu);
        SysMenu baseMenu = new SysMenu();
        baseMenu.setResName("顶级权限");
        baseMenu.setResId("0");
        baseMenu.setParentId("-1");
        list.add(baseMenu);
        return ResultTree.dataTree(list);
    }

    @PutMapping("/update")
    @ResponseBody
    public Result update(@RequestBody SysMenu sysMenu) {
        return Result.decide(sysMenuService.update(sysMenu));
    }

    @PostMapping("/save")
    @ResponseBody
    public Result save(@RequestBody SysMenu sysMenu) {
        SysMenuEntity entity = new SysMenuEntity();
        entity.setResId(IdWorker.getIdStr());
        entity.setResName(sysMenu.getResName());
        entity.setResType(sysMenu.getResType());
        entity.setResCode(sysMenu.getResCode());
        entity.setResUrl(sysMenu.getResUrl());
        entity.setOpenType(sysMenu.getOpenType());
        entity.setParentId(sysMenu.getParentId());
        entity.setIcon(sysMenu.getIcon());
        entity.setSort(sysMenu.getSort());
        entity.setRemark(sysMenu.getRemark());
        entity.setValidFlag(SimConstant.VALID_FLAG_1);
        return Result.decide(sysMenuService.save(entity));
    }

}
